<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('代码生成')"/>
</head>
<body class="pear-container">
<!-- 查询栏 -->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form search-form" id="searchForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">表名称</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" type="text" name="tableName" placeholder="" autocomplete="off">
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="form-search" type="button" class="pear-btn pear-btn-md pear-btn-primary" data-type="reload" lay-submit lay-filter="form-search">
                        <i class="layui-icon">&#xe615;</i>查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- 数据表头操作按钮 -->
<script type="text/html" id="data-toolbar">
    <button class="pear-btn pear-btn-md" lay-event="import">
        <i class="layui-icon">&#xe654;</i>导入
    </button>
</script>
<!-- 数据列表 -->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="data-table" lay-filter="data-table"></table>
    </div>
</div>
</body>
<!-- 列表操作按钮 -->
<script type="text/html" id="operate-bar">
    <button class="pear-btn pear-btn-sm pear-btn-warming" lay-event="preview"><i class="pear-icon pear-icon-browse"></i>
    </button>
    <button class="pear-btn pear-btn-sm pear-btn-primary" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-sm pear-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>
    </button>
    <button class="pear-btn pear-btn-sm pear-btn-success" lay-event="gencode"><i class="pear-icon pear-icon-export"></i>
    </button>
</script>

<th:block th:include="include :: footer"/>

<script>
    let prefix = "/gen/";

    layui.use(['common', 'layer', 'form', 'laydate', 'table', 'jquery'], function () {
        let $ = layui.jquery,
            layer = layui.layer,
            form = layui.form,
            common = layui.common,
            laydate = layui.laydate,
            table = layui.table;

        let active = {
            reload: function () {
                search(false);
            },
            remove: function (ids) {
                common.ajaxRemove(prefix + "remove", ids, function (res) {
                    active['reload'].call(this);
                });
            }
        };
        // 列字段
        let cols = [
            [
                {field: 'id', title: '表ID'},
                {field: 'tableName', title: '表名称'},
                {field: 'tableComment', title: '表描述'},
                {field: 'className', title: '实体类名称'},
                {field: 'tplCategory', title: '使用的模板'},
                {field: 'moduleName', title: '生成模块名'},
                {   field: 'genType',
                    title: '生成方式',
                    templet: function (row) {
                        if(row.genType == 0) {
                            return 'zip压缩包';
                        } else if(row.genType == 1) {
                            return '自定义路径';
                        }
                        return row.genType;
                    }
                 },
                {field: 'createTime', title: '创建时间', width: 160},
                {field: 'updateTime', title: '更新时间', width: 160},
                {title: '操作', align: 'center', width: 220, toolbar: '#operate-bar'}
            ]
        ]

        // 加载
        table.render({
            elem: '#data-table',
            url: prefix + "pageList",
            toolbar: '#data-toolbar',
            autoSort: false,
            cols: cols,
            page: true,
            skin: 'line',
            defaultToolbar: [
                'filter', 'exports'
            ]
        });

        form.on('submit(form-search)', function (data) {
            search(true, data.field);
        });

        table.on('toolbar(data-table)', function (obj) {
            switch (obj.event) {
                case 'import':
                    layer.open({
                        type: 2,
                        title: '导入表结构',
                        shade: false,
                        fixed: false,
                        area: ['1000px', '600px'],
                        content: prefix + "importTable",
                        btn: ["保存", "取消"],
                        yes: function (index, layero) {
                            layero.find('iframe')[0].contentWindow.submitHandler();
                        },
                        cancel: function (index, layero) {
                            layui.layer.close(index);
                            return false;
                        }
                    });
                    break;
            }
        });

        table.on('tool(data-table)', function (obj) {
            switch (obj.event) {
                case 'del':
                    active['remove'].call(this, obj.data.id);
                    break;
                case 'preview':
                    preview(obj.data.id);
                    break;
                case 'gencode':
                    let id = obj.data.id;
                    let tableName = obj.data.tableName;
                    let tableComment = obj.data.tableComment;
                    let genType = obj.data.genType;
                    layer.confirm('确定要生成' + tableName + '【' + tableComment + '】表代码吗？', function (index) {
                        if (genType == "0") {
                            location.href = prefix + "download/" + id;
                            layer.msg('执行成功,正在生成代码请稍后…', {
                                icon: 1
                            });
                        } else if (genType == "1") {
                            common.ajax.get(prefix + "genCode/" + id);
                            layer.msg('执行成功,请稍后刷新自定义代码生成目录…', {
                                icon: 1
                            });
                        }
                    });
                    break;
                case 'edit':
                    let url = prefix + 'view?id=' + obj.data.id;
                    parent.layui.tab.addTabOnlyByElem("content", {
                        id: obj.data.id,
                        title: "生成配置",
                        url: url,
                        close: true
                    });
                    break;
            }
        });
    });

    function search(param) {
        layui.table.reload('data-table', param);
    }

    function preview(id) {
        let preViewUrl = prefix + "preview/" + id;
        layui.layer.load(1, {
            shade: [0.2, '#fff']
        });
        layui.common.ajax.get(preViewUrl, function (result) {
            layui.layer.closeAll();
            if (result.code == 0) {
                let items = [];
                $.each(result.data, function (index, value) {
                    value = value.replace(/</g, "&lt;");
                    value = value.replace(/>/g, "&gt;");
                    let templateName = index.substring(index.lastIndexOf("/") + 1, index.length).replace(/\.vm/g, "");
                    if (!layui.common.equals("sql", templateName) && !layui.common.equals("tree.html", templateName) && !layui.common
                        .equals("sub-domain.java", templateName)) {
                        items.push({
                            title: templateName,
                            content: "<pre class=\"layui-code\">" + value + "</pre>"
                        })
                    }
                });
                top.layer.tab({
                    area: ['100%', '100%'],
                    shadeClose: true,
                    tab: items
                });
            } else {
                layui.layer.alert(result.msg);
            }
        });
    }
</script>
</html>
